package
{
	import com.xcopy.Screen;
	import com.xcopy.UIControlEnhencer;
	import fl.containers.UILoader;
	import flash.utils.setTimeout;
	import flash.display.InteractiveObject;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.FocusEvent;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	import flash.text.TextField;
	import flash.text.TextFormat;

	public class Slot25Menu extends Screen
	{
		protected var _boxMoney:DigitalBox           = new DigitalBox(this);
		protected var _money:Number                  = 0;
		protected var _chatMessage:UIControlEnhencer = new UIControlEnhencer(); // 系統訊息
		protected var _chatTalking:UIControlEnhencer = new UIControlEnhencer(); // 對話輸入框
		protected var _chatContent:UIControlEnhencer = new UIControlEnhencer(); // 對話內容
		private var _table:Slot25Table;

		public function get table():Slot25Table
		{
			return _table;
		}

		public function get main():NewBridge_25LineSlot
		{
			return table.main;
		}

		public function get core():Slot25Core
		{
			return main.core;
		}

		public function get debug():Slot25Debugger
		{
			return main.debug;
		}

		public function get client():FGS18Processer
		{
			return main.client;
		}

		public function set money(n:Number):void
		{
			_money = n;
			if (_boxMoney)
			{
				_boxMoney.target = n;
			}
		}

		public function get money():Number
		{
			if (_boxMoney)
			{
				return _boxMoney.target;
			}
			else
			{
				return _money;
			}
		}

		public function Slot25Menu(r:* = null)
		{
			super(r);
			_table = r as Slot25Table;
		}

		override public function set clip(mc:MovieClip):void
		{
			super.clip = mc;
			_boxMoney.clip = clip.oMoney;
			_boxMoney.TIME_STEP = 0.4;
			disable();
			_chatMessage.clip = clip.txtMessage;
			_chatContent.clip = clip.txtContent;
			_chatContent.setBackground(clip.oContentBack);
			_chatMessage.setBackground(clip.oMessageBack);
			_chatContent.clearFrame();
			_chatMessage.clearFrame();
			var tf:TextFormat = new TextFormat();
			tf.color = 0xFFFFFF;
			_chatContent.setFormat(tf);
			_chatMessage.setFormat(tf);
			money = _money;
			if (clip.oPic)
			{
				clip.oPic.oImage.addEventListener(Event.COMPLETE, onPicRead);
			}
			if (clip.btnHighQuality)
			{
				setClickEvent(clip.btnHighQuality, onQualityHigh);
				setClickEvent(clip.btnLowQuality, onQualityLow);
				setClickEvent(clip.btnNormalScreen, onScreenNormal);
				setClickEvent(clip.btnFullScreen, onScreenFull);
				setClickEvent(clip.btnSoundOn, onSoundOn);
				setClickEvent(clip.btnSoundOff, onSoundOff);
				setClickEvent(clip.btnDeposit, onPayment);
				setClickEvent(clip.btnRank, onRank);
			}
			full = false;
			sound = true;
			offMachine();
			openAD();
		}

		public function openAD():void
		{
			if (clip == null)
			{
				return;
			}
			clip.oImage.visible = true;
			clip.oImageBack.visible = true;
		}

		public function setAD(path:String):void
		{
			if (clip == null)
			{
				setTimeout(setAD, 400, path);
				return;
			}
			var loader:UILoader = clip.oImage as UILoader;
			loader.load(new URLRequest(path));
		}

		public function closeAD():void
		{
			if (clip == null)
			{
				return;
			}
			clip.oImage.visible = false;
			clip.oImageBack.visible = false;
		}

		// 進入機台
		public function onMachine():void
		{
			clip.btnRank.visible = true;
			clip.oRankBack.visible = true;
		}

		public function offMachine():void
		{
			clip.btnRank.visible = false;
			clip.oRankBack.visible = false;
		}

		public function onRank(e:MouseEvent):void
		{
			openURL('http://golden.goldengame.com.tw/rank/rank_bonus.php?type=13');
		}

		public function onPayment(e:MouseEvent):void
		{
			this.main.payGame();
		}

		public function onSoundOn(e:MouseEvent):void
		{
			this.core.sound = true;
		}

		public function onSoundOff(e:MouseEvent):void
		{
			this.core.sound = false;
		}

		public function onScreenNormal(e:MouseEvent):void
		{
			this.core.full = false;
		}

		public function onScreenFull(e:MouseEvent):void
		{
			this.core.full = true;
		}

		public function onQualityHigh(e:MouseEvent):void
		{
			this.core.quality = true;
		}

		public function onQualityLow(e:MouseEvent):void
		{
			this.core.quality = false;
		}

		public function onPicRead(e:Event):void
		{
			var content:MovieClip = MovieClip(clip.oPic.oImage.content);
			var frame:MovieClip   = content.oMahjongFrame;
			if (frame)
			{
				var rate:Number = 75 / frame.width;
				content.scaleX = rate;
				content.scaleY = rate;
				content.x -= frame.x * rate;
				content.y -= frame.y * rate;
			}
			else
			{
				//trace('無框');
			}
		}

		public function setPic(file:String):void
		{
			if (clip == null)
			{
				return;
			}
			if (clip.oPic)
			{
				clip.oPic.oImage.source = file;
			}
		}

		public function setImage(file:String):void
		{
			if (clip.oImage)
			{
				clip.oImage.source = file;
			}
		}

		public function addTalking(msg:String):void
		{
			_chatContent.addHtml('<font color="#FFFFFF">' + msg + '</font>');
		}

		public function addMessage(msg:String):void
		{
			_chatMessage.addHtml('<font color="#FFFFFF">' + msg + '</font>');
		}

		public function addToday(n:Number):void
		{
			scoreToday = n;
		}

		public function addTotal(n:Number):void
		{
			scoreTotal = n;
		}

		public function set quality(b:Boolean):void
		{
			if (clip.btnHighQuality)
			{
				clip.btnHighQuality.visible = !b;
				clip.btnLowQuality.visible = b;
			}
		}

		public function set full(b:Boolean):void
		{
			if (clip && clip.btnFullScreen)
			{
				clip.btnFullScreen.visible = !b;
				clip.btnNormalScreen.visible = b;
			}
		}

		public function set sound(b:Boolean):void
		{
			if (clip && clip.btnSoundOff)
			{
				clip.btnSoundOn.visible = !b;
				clip.btnSoundOff.visible = b;
			}
		}

		public function showCommercial(show:Boolean = true):void
		{
			if (show)
			{
				setAD(null);
			}
			else
			{
				closeAD();
			}
		}

		public function hideCommercial():void
		{
			showCommercial(false);
		}

		public function set scoreToday(n:Number):void
		{
			if (clip.txtToday)
			{
				clip.txtToday.text = n;
			}
		}

		public function set scoreTotal(n:Number):void
		{
			if (clip.txtTotal)
			{
				clip.txtTotal.text = n;
			}
		}
	}
}
